package pt.ua.helpdesk.client;


import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * This class extends a composite and will allow the
 * user to get a new password. It must contain a space
 * to introduce a valid email so that the system can
 * reset the password and send it to the email address
 * 
 * @author Amigos do Noddy
 */
public class LoginPass extends Composite {

	/**
	 * Class constructor. Loads the widgets into
	 * the main window
	 */
	public LoginPass() {

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		final HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_1);
		horizontalPanel_1.setSpacing(8);

		final Image image = new Image();
		horizontalPanel_1.add(image);
		image.setUrl("images/logo.png");

		final HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_2);
		horizontalPanel_2.setSpacing(2);

		final Label emailLabel = new Label("E-mail:");
		horizontalPanel_2.add(emailLabel);
		emailLabel.setStyleName("label");

		final TextBox txtEmail = new TextBox();
		horizontalPanel_2.add(txtEmail);
		txtEmail.setWidth("250");
		txtEmail.setStyleName("textbox");

		final HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_4);
		horizontalPanel_4.setSpacing(4);

		final Button btnVoltar = new Button();
		horizontalPanel_4.add(btnVoltar);
		btnVoltar.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				Index.voltar();
			}
		});
		btnVoltar.setStyleName("buttonVoltar");
		btnVoltar.setText("« Voltar");

		final HTML htmlAlerta = new HTML("");
		
		final Button activarButton = new Button();
		horizontalPanel_4.add(activarButton);
		activarButton.addClickListener(new ClickListener() {
			public void onClick(final Widget sender) {
				boolean alerta = false;
				int c = 0;
							
				if(txtEmail.getText().equals(""))
				{
					if(c == 0)
					{
						htmlAlerta.setHTML("<u>ALERTA:</u><br>");
						c++;
					}
					alerta = true;
					htmlAlerta.setHTML(htmlAlerta.getHTML() + "» tem de introduzir o E-mail! <br>");
				}
				else
					if(!txtEmail.getText().matches(".+@.+\\.[a-z]+"))
					{
						if(c == 0)
						{
							htmlAlerta.setHTML("<u>ALERTA:</u><br>");
							c++;
						}
						alerta = true;
						htmlAlerta.setHTML(htmlAlerta.getHTML() + "» o e-mail não é válido! <br>");
					}
							
				if(!alerta)
				{
					htmlAlerta.setHTML("a processar dados...");
					ServiceLogin.Util.getInstance().novapass(txtEmail.getText(), new NovaPassAsyncCallback(htmlAlerta));
				}

			}
		});
		activarButton.setStyleName("button");
		activarButton.setText("Nova Palavra-Chave");

		final HorizontalPanel horizontalPanel_5 = new HorizontalPanel();
		verticalPanel.add(horizontalPanel_5);
		horizontalPanel_5.setSpacing(2);

		
		horizontalPanel_5.add(htmlAlerta);
		htmlAlerta.setStyleName("erro");
	}
	
	/**
	 * An asynchronous callback is the primary interface that a caller
	 * must implement to receive a response from a remote procedure call
	 * 
	 * This class is the implementation of AsyncCallback for the new
	 * password service
	 * 
	 * @author Amigos do Noddy
	 */
	  static class NovaPassAsyncCallback implements AsyncCallback {
		  HTML html;
		  
		  /**
		   * Class constructor
		   * @param html Widget that can contain arbitrary HTML
		   */
		  public NovaPassAsyncCallback(HTML html) {
			  this.html= html;
		  }
		  
		  /**
		   * Success handler
		   */
		  public void onSuccess(Object object) {
			  Boolean ret = (Boolean)object;
			  String message = "";
			  
			  if(ret.booleanValue())
				  message = "Foi enviada uma nova Palavra-chave para o seu E-mail! <br>";
			  else
				  message = "O E-mail não se encontra na base de dados ou a conta não está activa!";
			  

    		  DOM.setInnerHTML(html.getElement(), message);
    		  
		  }
		  
		  /**
		   * Failure handler
		   */
		  public void onFailure(Throwable throwable) {
			  String message = "ocorreu um erro inesperado!";
    		  DOM.setInnerHTML(html.getElement(), message);
		  }
	  }
}
